package com.hankol.message

import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.BaseConstant
import com.baselibrary.Constants
import com.baselibrary.activity.BaseActivity
import com.baselibrary.manager.SensorsDataManager
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ARouterUtils
import com.baselibrary.utils.DoubleClickHelper
import com.baselibrary.utils.EmptyViewUtil
import com.hankol.message.adapter.LikeAndCollectAdapter
import com.hankol.message.bean.LikeAndCollectBean
import com.hankol.message.databinding.ActivityLikeAndCollectBinding
import com.hankol.message.viewmodel.HomeMessageViewModel
import java.util.*

/**点赞和收藏
 * @author: merbng
 * @time: 2022/3/25 10:15
 * @description:
 */
@Route(path = ActivityRouter.MESSAGE.LIKE_AND_COLLECT)
class LikeAndCollectActivity : BaseActivity() {

    private lateinit var viewModel: HomeMessageViewModel
    private lateinit var binding: ActivityLikeAndCollectBinding
    val adapter: LikeAndCollectAdapter by lazy { LikeAndCollectAdapter() }
    var page = 1
    private var hasnext: Boolean = false
    override fun getLayoutId(): Int {
        return R.layout.activity_like_and_collect
    }

    override fun init() {
        binding = getDataBinding()
        viewModel = getActivityViewModelProvider(this).get(HomeMessageViewModel::class.java)
        binding.tvTitle.text = "点赞和收藏"
        binding.backImageView.setOnClickListener { finish() }
        binding.rvLike.setAdapter(adapter)
        adapter.setOnItemClickListener { adapter, view, position ->
            if (!DoubleClickHelper.isOnDoubleClick()) {
                val bean = adapter.data[position] as LikeAndCollectBean
                //动态类型：1-图文，2-视频
                if (bean.timelineShowtype == 1) {
                    val hashMap = HashMap<String, Any>()
                    hashMap["id"] = "${bean.timelineId}"
                    hashMap["commentId"] = bean.commentId//评论id，高亮评论的那条
                    hashMap["source"] = BaseConstant.POINT_DYNAMIC_GIVE_COL

                    ARouterUtils.jump(ActivityRouter.ACTIVITY.IMAGE_DETAIL, hashMap)
                } else {//视频动态
                    val hashMap = HashMap<String, Any>()
                    hashMap["id"] = bean.timelineId
                    hashMap["bid"] = bean.timelineId
                    hashMap["JumpType"] = 0
                    hashMap["commentId"] = bean.commentId//评论id，高亮评论的那条
                    hashMap["source"] = BaseConstant.POINT_DYNAMIC_GIVE_COL
                    ARouterUtils.jump(ActivityRouter.ACTIVITY.READ_VIDEO_DETAIL, hashMap)
                }
            }
        }
        adapter.setOnItemChildClickListener { adapter, view, position ->
            if (!DoubleClickHelper.isOnDoubleClick()) {
                val bean = adapter.data[position] as LikeAndCollectBean
                when (view.id) {
                    R.id.img_heard, R.id.ll_nick -> {
                        val hashMap = HashMap<String, Any>()
                        hashMap[BaseConstant.USER_ID] = bean.uid
                        ARouterUtils.jump(ActivityRouter.MINE_CENTER.USER_INFO, hashMap)
                    }
                }
            }
        }
        binding.smartRefresh.setOnRefreshListener {
            page = 1
            loadData()
        }
        binding.smartRefresh.setOnLoadMoreListener {
            if (hasnext) {
                loadData()
            } else {
                binding.smartRefresh.finishLoadMore()
            }
        }
        observerCallBck()
        loadData()
        //获取页面开始时间
        SensorsDataManager.instance.onCreateTime()
    }

    private fun observerCallBck() {
        viewModel.dataListLD.observe(this, Observer {
            hideLoadingDialog()
            binding.smartRefresh.finishRefresh()
            if (it == null || it.data?.size == 0) {
                //无数据
                if (page == 1) {
                    adapter.emptyView = EmptyViewUtil.getEmptyView(
                        Constants.TYPE_LIKE_COMMENT,
                        binding.rvLike,
                        "无相关点赞信息"
                    )
                } else {
                    binding.smartRefresh.finishLoadMore()
                }
            } else {
                if (it.data != null) {
                    hasnext = it.totalPage != adapter.data.size
                    binding.smartRefresh.finishLoadMore()
                    if (page == 1) {
                        adapter.setNewData(it.data)
                    } else {
                        adapter.addData(it.data)
                    }
                    page++
                }
            }
        })
    }

    fun loadData() {
        showLoadingDialog()
        viewModel.reqLikeAndCollectList(page)
    }

    override fun onDestroy() {
        super.onDestroy()
        //获取页面结束时间
        SensorsDataManager.instance.onDestroyTime()
        if (SensorsDataManager.instance.getBrowseTime() != 0L) {
            SensorsDataManager.instance.brookPageView("赞和收藏")
        }
    }
}